package com.jinyu.jyly.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.jinyu.jyly.R;
import com.jinyu.jyly.bean.GetPriceInfo;
import com.jinyu.jyly.ui.mine.passageway.PassageWayTabFragment;
import com.jinyu.jyly.view.CheckableLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * 推广页面
 */
public class ExtensionAdapter extends RecyclerView.Adapter<ExtensionAdapter.ViewHolder> {

    private Context mContext;
    private List<GetPriceInfo> mListData = new ArrayList<>();

    private OnTagItemClickListener OnTagItemClickListener;
    public void setOnItemClickListener(OnTagItemClickListener OnTagItemClickListener) {
        this.OnTagItemClickListener = OnTagItemClickListener;
    }

    public ExtensionAdapter(Context mContext, List<GetPriceInfo> mListData) {
      //  mListData = new ArrayList<>();
        this.mContext = mContext;
        this.mListData = mListData;
    }
    public void update(List<GetPriceInfo> list){
        if(list != null && list.size() > 0){
            mListData.addAll(list);
            notifyDataSetChanged();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        CheckableLayout rootLayout;
        private final TextView tv_price_rmb;
        private final TextView tv_price;

        public ViewHolder(View itemView) {
            super(itemView);

            rootLayout = (CheckableLayout) itemView.findViewById(R.id.root_layout);
            tv_price_rmb = itemView.findViewById(R.id.tv_price_rmb);
            tv_price = itemView.findViewById(R.id.tv_price);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        if(mContext == null){
            mContext = viewGroup.getContext();
        }
        View view  = LayoutInflater.from(mContext).inflate(R.layout.item_extension_recycler,viewGroup,false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Set<Integer> positionSet = PassageWayTabFragment.positionSet;
        //检查set里是否包含position，包含则显示选中的背景色，不包含则反之
        if (positionSet.contains(position)) {
            holder.rootLayout.setChecked(true);

            holder.tv_price_rmb.setTextColor(mContext.getResources().getColor(R.color.white));
            holder.tv_price.setTextColor(mContext.getResources().getColor(R.color.white));
        } else {
            holder.rootLayout.setChecked(false);
            holder.tv_price_rmb.setTextColor(mContext.getResources().getColor(R.color.gray06));
            holder.tv_price.setTextColor(mContext.getResources().getColor(R.color.gray06));
        }

        GetPriceInfo dataBean = mListData.get(position);

        holder.tv_price.setText(dataBean.getName());
        if(OnTagItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    OnTagItemClickListener.OnTagItemClick(holder.itemView, pos);
                    holder.rootLayout.setChecked(true);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mListData != null ? mListData.size() : 0;
    }

    public interface OnTagItemClickListener {
        //单点监听
        void OnTagItemClick(View view, int position);
        //长点监听
        void OnTagItemLongClick(View view, int position);
    }

}
